/**
* @authors <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* @version 0.9
* Copyright 2010
* 
* The MIT License
* Copyright (c) 2010 <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package  com.ng.utils {
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	
	public class c_FileReader extends EventDispatcher{
		
		public static var FILE_LOADED:String = 'FILE_LOADED';
		
		private var __FileData:String;
		private var __File:File;
		
		public function c_FileReader() {
			var fileToOpen:File = new File();
			var txtFilter:FileFilter = new FileFilter("Text", "*.xml");			
			try {
				fileToOpen.browseForOpen("Open", [txtFilter]);
				fileToOpen.addEventListener(Event.SELECT, OnFileSelected);
			} catch (error:Error){
				c.e(error.message);
			}
		}
		public function get FileData():String {
			return __FileData;
		}
		public function get file():File {
			return __File;
		}
		private function OnFileSelected(event:Event):void {
			c.d2(event + ' ' + event.target);
			var stream:FileStream = new FileStream();
			__File = event.target as File;
			
			stream.open(__File, FileMode.READ);
			c.d2("File: " + __File.name + __File.nativePath);
			c.d2(__File.parent.nativePath.toString());
			__FileData = stream.readUTFBytes(stream.bytesAvailable);			
			dispatchEvent(new Event(FILE_LOADED));
		}		
	}
}